package com.zhaoc.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhaoc.domain.take_delivery.WayBill;
import com.zhaoc.service.IWaybillService;
import com.zhaoc.web.base.BaseAction;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;


/**
 * 运单管理
 * <p>title:com.zhaoc.web.action</p>
 * date:2018/8/16 0016
 * time:19:54
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {

    private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);

    @Autowired
    private IWaybillService waybillService;

    @Action(value = "waybill_save", results = {@Result(name = SUCCESS, type = "json")})
    public String save() {
        //先定义一个map集合存放结果
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder()
                    .getId() == 0)) {
                model.setOrder(null);
            }
            waybillService.save(model);
            //保存成功
            result.put("success", true);
            result.put("msg1", "保存运单成功,运单号为:" + model.getWayBillNum());
            LOGGER.info("保存运单成功,运单号为:" + model.getWayBillNum());
        } catch (Exception e) {
            e.printStackTrace();
            //保存失败
            result.put("success", false);
            result.put("msg1", "保存运单失败");
            LOGGER.info("保存运单成功,运单号为:" + model.getWayBillNum(), e);
        }
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }

    @Action(value = "waybill_pageQuery", results = {@Result(name = SUCCESS, type = "json")})
    public String pageQuery() {
        //无条件查询,并按ID进行倒序排序
        Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
        Page<WayBill> pageData = waybillService.findPageData(model,pageable);
        //将结果也如栈顶返回
        pushPageDataToValueStack(pageData);
        return SUCCESS;
    }

    @Action(value = "order_findBywayBillNum", results = {@Result(name = SUCCESS, type = "json")})
    public String findByWayBillNum() {
        WayBill wayBill = waybillService.findByWayBillNum(model.getWayBillNum());
        Map<String, Object> result = new HashMap<String, Object>();
        if (wayBill == null) {
            //运单号不存在
            result.put("success", false);
        } else {
            //查询到对应的订单
            result.put("success", true);
            result.put("wayBillData", wayBill);
        }
        ActionContext.getContext().getValueStack().push(result);

        return SUCCESS;
    }
}
